package cn.senjoeson.triper.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.senjoeson.mylibrary.utils.ResourcesUtils;
import cn.senjoeson.mylibrary.utilschanged.GlideUtils;
import cn.senjoeson.mylibrary.utilschanged.LogUtils;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.activity.DestinationActivity;
import cn.senjoeson.triper.base.GreenDaoManager;
import cn.senjoeson.triper.bean.PlaceBean;
import cn.senjoeson.triper.dao.PlaceBeanDao;
import cn.senjoeson.triper.utils.IdsUtils;

/**
 * Created by senjoeson on 2018/1/7.
 */

public class TripContentAdapter extends RecyclerView.Adapter {


    private List<PlaceBean> mDatas;
    private Context mContext;
    private OnChangeListener mOnChangeListener;

    public TripContentAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setDatas(List<PlaceBean> mDatas) {
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trip_content_list_item_0, parent, false);
            return new HeadViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trip_content_list_item_1, parent, false);
            return new ViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mDatas.get(position).getFlag() ? 0 : 1;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final PlaceBean placeBean = mDatas.get(position);
        if (holder instanceof HeadViewHolder) { //第一种视图类型
            final HeadViewHolder headViewHolder = (HeadViewHolder) holder;
            GradientDrawable gradientDrawable = (GradientDrawable) headViewHolder.mTimeclock.getBackground();
            gradientDrawable.setColor(ResourcesUtils.getColor(R.color.orange_color));
            headViewHolder.mTimeclock.setText(placeBean.getTime());
            headViewHolder.mDesc.setText(placeBean.getPlaceName());

            headViewHolder.mTimeclock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showSetTimeDialog(headViewHolder.mTimeclock, placeBean);
                }
            });
        } else {        //第二种视图类型
            final ViewHolder viewHolder = (ViewHolder) holder;
            GradientDrawable gradientDrawable = (GradientDrawable) viewHolder.mTimeclock.getBackground();
            gradientDrawable.setColor(ResourcesUtils.getColor(R.color.green));

            //设置数据
            viewHolder.mTimeclock.setText(placeBean.getTime());
            viewHolder.mLocationName.setText(placeBean.getPlaceName());
            GlideUtils.loadImage(mContext, placeBean.getImageUrl(), viewHolder.mLocatonImage);
          /*  Glide.with(AppUtils.getContext())
                    .load(placeBean.getImageUrl())
                    .placeholder(R.drawable.ic_launcher_background)
                    .crossFade()
                    .into();*/
            //设置默认是隐藏的
            viewHolder.mSecondlayout.setVisibility(View.GONE);

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = viewHolder.itemView.getContext();
                    Intent intent = new Intent(context, DestinationActivity.class);
                    long dayid = placeBean.getDayid();
                    //todo 此值修改可能出现问题
                    long placeId = IdsUtils.getOriginId(placeBean.getId(), placeBean.getPlanid(), placeBean.getDayid());
                    LogUtils.d(placeId + "跳转的");
                    intent.putExtra("placeId", placeId);

                    context.startActivity(intent);
                }
            });
            viewHolder.mTimeclock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showSetTimeDialog(viewHolder.mTimeclock, placeBean);
                }
            });
            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                   /* Dialog dialog = DialogUtils.showCommonDialog(mContext, "你确定要删除该条数据么?", "确定", "取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            deleteItem(placeBean);

                            dialog.dismiss();
                        }
                    });
                    dialog.show();*/
                    viewHolder.mSecondlayout.setVisibility(View.VISIBLE);

                    return true;
                }
            });
            viewHolder.mSecondlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewHolder.mSecondlayout.setVisibility(View.GONE);
                }
            });
            viewHolder.mDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteItem(placeBean);
                    PlaceBeanDao placeBeanDao = GreenDaoManager.getInstance().getNewSession().getPlaceBeanDao();
                    placeBeanDao.delete(placeBean);
                }
            });

            viewHolder.mChange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //可以把该地点移动到其他的活动天
                    // showChangeDialog();
                    if (mOnChangeListener != null) {
                        mOnChangeListener.onChange(placeBean, viewHolder.getAdapterPosition());
                        viewHolder.mSecondlayout.setVisibility(View.GONE);
                    }
                }
            });
        }
    }


    /**
     * 删除条目
     *
     * @param placeBean 位置对象
     */
    public void deleteItem(PlaceBean placeBean) {
        if (mDatas.contains(placeBean)) {
            mDatas.remove(placeBean);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }


    /***
     * 显示修改时间的dialog
     *
     * @param mTv       需要被改变内容的控件
     * @param placeBean 需要被更新的bean
     */
    public void showSetTimeDialog(final TextView mTv, final PlaceBean placeBean) {

        final Dialog dialog = new Dialog(mContext);
        dialog.setContentView(R.layout.select_time_dialog);
        dialog.setTitle("请选择时间");
        final TimePicker timePicker = dialog.findViewById(R.id.timePicker);
        timePicker.setIs24HourView(true);
        TextView mCancel = dialog.findViewById(R.id.cancel);
        TextView mConfirm = dialog.findViewById(R.id.comfirm);
        final PlaceBeanDao placeBeanDao = GreenDaoManager.getInstance().getNewSession().getPlaceBeanDao();

        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    LogUtils.d("小时数：" + timePicker.getHour() + "分钟数:" + timePicker.getMinute());
                    // LogUtils.d(timePicker.get);
                    String time = transTime(timePicker.getHour()) + ":" + transTime(timePicker.getMinute());
                    mTv.setText(time);
                    placeBean.setTime(time);
                    placeBeanDao.update(placeBean);

                }
                dialog.dismiss();


            }
        });
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                LogUtils.d("时间改变回调");
            }
        });
    }

    /**
     * 规范时间输出格式
     *
     * @param time
     * @return
     */
    public String transTime(int time) {
        if (time < 10) {
            return "0" + time;
        }
        return time + "";
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.timeclock)
        TextView mTimeclock;
        @BindView(R.id.desc)
        TextView mDesc;

        public HeadViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.timeclock)
        TextView mTimeclock;
        @BindView(R.id.locatonImage)
        ImageView mLocatonImage;
        @BindView(R.id.locationName)
        TextView mLocationName;
        @BindView(R.id.change)
        ImageView mChange;
        @BindView(R.id.delete)
        ImageView mDelete;
        @BindView(R.id.secondlayout)
        RelativeLayout mSecondlayout;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface OnChangeListener {
        void onChange(PlaceBean placeBean, int adapterPosition);
    }

    public void setOnChangeListener(OnChangeListener onChangeListener) {
        if (onChangeListener != null) {
            mOnChangeListener = onChangeListener;
        }
    }
}
